home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / Drop•MPSR ƒ / Drop•MPSR.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  7.8 KB  |  383 lines  |  [TEXT/KAHL]

  1. /*
  2.     Drop•MPSR.c
  3.     
  4.     This is the code for a small application which handles the modification of the MPSR resource for
  5.     editor systems.
  6.     
  7.     Many times I will download code which I want to use with (insert your editor's name here).  However,
  8.     the code is personalized for the person that used it (i.e. I like geneva-9 for programming, others prefer
  9.     something different).  Well, all of this information is stored in the MPSR resource in each of the text
  10.     files.
  11.     
  12.     So I wanted an appl that I could drop the downloaded files on to set them up the way I want them
  13.     right away; therefore I created this application.
  14.     
  15.     But I took the code one step further; not only does the MPSR resource get fixed, but this appl will set
  16.     up a bunch of things for you (i.e. creator).  Another thing it does is stack all of the windows for you.
  17.     If you drop a bunch of files on the app at one time, the windows will be staggered automatically.
  18.     
  19. */
  20.  
  21. #include <LowMem.h>
  22.  
  23. #include "Drop•MPSR.h"
  24. #include "MPSR Resource.h"
  25.  
  26. #include "DSUtils.h"
  27.  
  28. #include "SpinLib.h"
  29.  
  30. Rect gMainScrn;
  31.  
  32. Rect gOutRect,gInRect; // for storing the sizes of the zoom in and out rects.
  33. short gSelf;
  34. unsigned long gModTime;
  35. Handle gMPSR;
  36. unsigned short gLeft,gRight,gTop,gBottom,gGoDown,gGoLeft;
  37. OSType gCreator;
  38.  
  39. /*
  40.     MPSR_Init
  41.     
  42.     Initializes our stuff.
  43. */
  44. Boolean MPSR_Init(){
  45.     Boolean ret;
  46.     OSErr err;
  47.     GDHandle mainDev;
  48.     Handle h;
  49.     PrefHand pref;
  50.     
  51.     ret=true;
  52.     
  53.     gSelf=CurResFile();
  54.     
  55.     SpinInit();
  56.     
  57.     // get our prefs...
  58.     h=Get1Resource('•prf',editResID);
  59.     if (h!=(Handle)0){
  60.         DetachResource(h);
  61.         HLock(h);
  62.         
  63.         pref=(PrefHand)h;
  64.         gLeft=(*pref)->left;
  65.         gRight=(*pref)->right;
  66.         gTop=(*pref)->top;
  67.         gBottom=(*pref)->bottom;
  68.         
  69.         gGoDown=(*pref)->shiftdown;
  70.         gGoLeft=(*pref)->shiftleft;
  71.         
  72.         gCreator=(*pref)->creator;
  73.         
  74.         HUnlock(h);
  75.         DisposeHandle(h);
  76.     } else {
  77.         gCreator='KAHL';
  78.         gLeft=gRight=gTop=gBottom=2;
  79.         
  80.     }
  81.     
  82.     // get the main screen's rectangle
  83.     mainDev=GetMainDevice();
  84.     HLock((Handle)mainDev);
  85.     CopyRect(&((**((*mainDev)->gdPMap)).bounds),&gMainScrn);
  86.     
  87.     if (gMainScrn.top<0)
  88.         gMainScrn.top=0;
  89.         
  90.     // cut out the menu bar
  91.     gMainScrn.top += LMGetMBarHeight();
  92.     gMainScrn.top += 16;
  93.     
  94.     // add the prefered gaps...
  95.     gMainScrn.left+=gLeft;
  96.     gMainScrn.right-=gRight;
  97.     gMainScrn.top+=gTop;
  98.     gMainScrn.bottom-=gBottom;
  99.     
  100.     return ret;
  101. }
  102.  
  103. /*
  104.     MPSR_Cleanup
  105.     
  106.     Cleans up our stuff.
  107. */
  108. void MPSR_Cleanup(){
  109.     
  110.     SpinCleanup();
  111. }
  112.  
  113. /*
  114.     CopyRect
  115.     
  116.     Copies one rect to another.
  117. */
  118. void CopyRect(Rect* from,Rect* to){
  119.     to->top=from->top;
  120.     to->bottom=from->bottom;
  121.     to->left=from->left;
  122.     to->right=from->right;
  123. }
  124.  
  125. /*
  126.     MPSR_Preflight
  127.     
  128.     Preflight handling of the documents.  We reset the rectangles for each group of files.
  129. */
  130. void MPSR_Preflight(short count,Handle* dataHdl){
  131.     OSErr err;
  132.     short current;
  133.     
  134.     SpinStart(1);
  135.     
  136.     // get the MPSR resource from the application
  137.     current=CurResFile();
  138.     UseResFile(gSelf);
  139.     gMPSR=Get1Resource(editResType,editResID);
  140.     err=ResError();
  141.     UseResFile(current);
  142.     
  143.     if (err!=noErr){
  144.         gMPSR=(Handle)0;
  145.     } else {
  146.         // got the handle, ok to continue...
  147.         DetachResource(gMPSR);
  148.         MoveHHi(gMPSR);
  149.         HLock(gMPSR);
  150.         
  151.         // set up the zooming rectangles.
  152.         CopyRect(&gMainScrn,&gOutRect);
  153.         gOutRect.top +=4;
  154.         gOutRect.right -=51;
  155.         
  156.         // now set the zoom-in rect
  157.         CopyRect(&gOutRect,&gInRect);
  158.         
  159.         // modify the zoom-in rect some more...
  160.         gInRect.right=gInRect.left +80;
  161.         gInRect.bottom=gInRect.top+60;
  162.         
  163.         ReadDateTime(&gModTime);
  164.         
  165.         // now we're all set...
  166.     }
  167. }
  168.  
  169. /*
  170.     MPSR_Postflight
  171.     
  172.     Postflight handling of the documents.
  173. */
  174. void MPSR_Postflight(short count,Handle dataHdl){
  175.     
  176.     // ditch the resource now...
  177.     if (gMPSR!=(Handle)0){
  178.         HUnlock(gMPSR);
  179.         DisposeHandle(gMPSR);
  180.     }
  181.     
  182.     SpinStop();
  183. }
  184.  
  185. /*
  186.     MPSR_Process
  187.     
  188.     Process one document.
  189. */
  190. OSErr MPSR_Process(FSSpecPtr theSpec){
  191.     OSErr err=noErr;
  192.     FInfo info;
  193.     EditRSRCHdl mpsr=(EditRSRCHdl)gMPSR;
  194.     
  195.     err=FSpGetFInfo(theSpec,&info);
  196.     
  197.     if (err==noErr){
  198.         if (info.fdType!='TEXT'){
  199.             return noErr; // don't do anything to this file...
  200.         }
  201.     } else
  202.         return err;
  203.     
  204.     // now update file for the changes...
  205.     
  206.     if (info.fdCreator!=gCreator)
  207.         SetCreator(theSpec,gCreator);
  208.     
  209.     // make the changes to the local MPSR resource handle...
  210.     CopyRect(&gInRect,&((*mpsr)->windrsrc.openrect));
  211.     CopyRect(&gOutRect,&((*mpsr)->windrsrc.closerect));
  212.     
  213.     // fix up the in and out rects for the next file...
  214.     gOutRect.top+=gGoDown;
  215.     
  216.     if (gOutRect.top>gOutRect.bottom-60){
  217.         gOutRect.left+=gGoLeft;
  218.         
  219.         gOutRect.right+=gGoLeft;
  220.         
  221.         if (gOutRect.right>gMainScrn.right)
  222.             gOutRect.right=gMainScrn.right;
  223.             
  224.         gOutRect.top=gMainScrn.top+4;
  225.     }
  226.     
  227.     gInRect.left+=85;
  228.     gInRect.right+=85;
  229.     if (gInRect.right>gMainScrn.right)
  230.         gInRect.right=gMainScrn.right;
  231.     
  232.     if (gInRect.left>gInRect.right-60){
  233.         gInRect.left=gMainScrn.left;
  234.         gInRect.top+=65;
  235.         gInRect.right=gInRect.left+80;
  236.         gInRect.bottom=gInRect.top+60;
  237.     }
  238.     
  239.     // change the file's MPSR resource...
  240.     SetMPSRRes(theSpec);
  241.     
  242.     err=ForceFinderUpdate(theSpec,false);
  243.     return err;
  244. }
  245.  
  246. /*
  247.     SetCreator
  248.     
  249.     Sets the creator for a file.
  250. */
  251. void SetCreator(FSSpec* fsp,OSType type){
  252.     FInfo info;
  253.     OSErr err;
  254.     
  255.     err=FSpGetFInfo(fsp,&info);
  256.     
  257.     if (info.fdCreator!=type){
  258.         
  259.         info.fdCreator=type;
  260.         
  261.         err=FSpSetFInfo(fsp,&info);
  262.         
  263.         if (err!=noErr){
  264.             DSLocalError("\pError changing the file's creator.",err);
  265.         }
  266.     }
  267. }
  268.  
  269. /*
  270.     SetMPSRRes
  271.     
  272.     Set the MPSR resource to the one out of the application's resource fork.
  273. */
  274. void SetMPSRRes(FSSpecPtr myFSSPtr){
  275.     Handle h;
  276.     short fref;
  277.     OSErr rerr;
  278.     EditRSRCHdl mpsr=(EditRSRCHdl)gMPSR;
  279.     long date=gModTime;
  280.     
  281.     fref=FSpOpenResFile(myFSSPtr,fsRdWrPerm);
  282.     rerr=ResError();
  283.     
  284.     if (fref==-1){ // there was an error...
  285.         
  286.         if ((rerr==mapReadErr)||(rerr==-39)){// no resource fork.  Create the res fork and re-open
  287.             FSpCreateResFile(myFSSPtr,gCreator,'TEXT',smSystemScript);
  288.             rerr=ResError();
  289.             
  290.             if (rerr!=noErr){
  291.                 DSLocalError("\pUnable to create the resource fork for this file.",rerr);
  292.                 return;
  293.             }
  294.             
  295.             fref=FSpOpenResFile(myFSSPtr,fsRdWrPerm);
  296.             rerr=ResError();
  297.             
  298.             if (fref==-1){
  299.                 DSLocalError("\pSerious program malfunction.",rerr);
  300.                 return;
  301.             }
  302.         } else {
  303.              if (rerr!=-39){
  304.                 // some kind of weird funky error.
  305.                 DSLocalError("\pWeird error encountered...",rerr);
  306.                 return;
  307.             } else { // weird number -39
  308.                 SysBeep(10);
  309.             }
  310.         }
  311.     } else { // it opened ok.  This could mean that there is already an MPSR resource.
  312.         // check to see if there is one and remove it if so
  313.         
  314.         UseResFile(fref);
  315.         
  316.         h=Get1Resource(editResType,editResID);
  317.         rerr=ResError();
  318.         
  319.         if ((h!=(Handle)0) && (rerr==noErr)) {    // then the resource exists
  320.             EditRSRCHdl tmp=(EditRSRCHdl)h;
  321.             
  322.             RmveResource(h); // mark it removed from the res file...
  323.             
  324.             // we must use the date out of this file, otherwise it could prompt some compilers to recompile
  325.             // unchanged source code files.  So get the handle's date before disposing of it...
  326.             HLock(h);
  327.             date=(*tmp)->selrsrc.date;
  328.             HUnlock(h);
  329.             
  330.             DisposeHandle(h);
  331.             
  332.             UpdateResFile(fref);
  333.         }
  334.     }
  335.     // make sure we are using the correct file
  336.     UseResFile(fref);
  337.     
  338.     // set the correct modification time
  339.     (*mpsr)->selrsrc.date=date;
  340.     
  341.     // temporarily remove the lock on the handle...
  342.     HUnlock(gMPSR);
  343.     
  344.     // add the resource to the file
  345.     AddResource(gMPSR,editResType,editResID,"\pDrop•MPSR Resource");
  346.     rerr=ResError();
  347.     
  348.     if (rerr!=noErr){
  349.         DSLocalError("\pErrors adding MPSR resource.",rerr);
  350.         CloseResFile(fref);
  351.         return;
  352.     }
  353.     
  354.     // update the file
  355.     UpdateResFile(fref);
  356.     
  357.     // detach the resource again (so we can re-use it)...
  358.     DetachResource(gMPSR);
  359.     
  360.     // replace the lock
  361.     MoveHHi(gMPSR);
  362.     HLock(gMPSR);
  363.     
  364.     // close the res file and exit...
  365.     CloseResFile(fref);
  366. }
  367.  
  368. /*
  369.     DSLocalError
  370.     
  371.     Local error function, should put up an alert with the error message.
  372. */
  373. void DSLocalError(StringPtr sp,OSErr err){
  374.     #define    kAlertID    200
  375.     Str255    errorStr;
  376.  
  377.     NumToString ( err, errorStr );
  378.     ParamText ( sp,  errorStr, NULL, NULL );
  379.     CenterAlert ( kAlertID );
  380.     (void) Alert ( kAlertID, NULL );
  381. }
  382.  
  383.